*************************************************************************************************
*********************************** REPLICATION MATERIALS (I) *********************************** 
******************* Identities, causal antecedents and support for secession ********************
*************************************************************************************************

**Use database 
use CEO2014_20, clear

capture cd Set own path 

***************************************** RECODES *****************************************

**National-regional identity [5 categories]
recode SENTIMENT_PERTINEN__A (1=1 "Only Spanish") (2=2 "More Spanish") (3=3 "Dual identity") (4=4 "More Catalan") (5=5 "Only Catalan") (98/99=.), gen(ins)
label variable ins "Identity"

**National-regional identity [4 categories]
recode SENTIMENT_PERTINEN__A (1/2=1 "Only and More Spanish than Catalan") (3=2 "Dual identity") (4=3 "More Catalan") (5=4 "Only Catalan") (98/99=.), gen(ins4cat)
label variable ins4cat "Identity"

recode SENTIMENT_PERTINEN__A (1/2=1 "spanish") (3=2 "dual") (4=3 "morecat") (5=4 "catalan") (98/99=.), gen(ins4catb)
label variable ins4cat "Identity"
recode ins (5=1 "Only Catalan") (4=0.6666) (3=0.3333) (1 2=0 "Only Spanish"), gen(ins01)
recode ins (5=1 "Only Catalan") (4=0.75) (3=0.5) (2=0.25) (1=0 "Only Spanish"), gen(ins01_5)
recode ins (4 5=1 "Catalan") (1 2 3=0 "Spanish"), gen(ins2)

**National-regional identity: dummies
tab ins4cat, gen(ins4cat)

**Language
recode LLENGUA_PRIMERA (2/80=0 "Language: Spanish and others") (1=1 "Language: Catalan") (else=.), gen(language)
label variable language "Language: Catalan"

recode LLENGUA_PRIMERA (1=2 "Catalan") (2=3 "Spanish") (3=4 "Bilingual") (4/80=1 "Other languages") (*=.), gen(lang4)
tab lang4, gen(lang4_)

**Origin
recode LLOC_NAIX (1=1 "Origin: Born in Catalonia") (2/4=0 "Rest of Spain and others") (else=.), gen(origin)
label var origin "Origin: Born in Catalonia"

**Ancestry
gen ancestry=.
replace ancestry=3 if LLOC_NAIX_PARE==1 & LLOC_NAIX_MARE==1
replace ancestry=2 if LLOC_NAIX_PARE!=1 & LLOC_NAIX_MARE==1
replace ancestry=2 if LLOC_NAIX_PARE==1 & LLOC_NAIX_MARE!=1
replace ancestry=1 if LLOC_NAIX_PARE!=1 & LLOC_NAIX_MARE!=1
label variable ancestry "Ancestry"
label define ancestry 1 "None of the parents born in Catalonia" 2 "One parent born in Catalonia" 3 "Two parents born in Catalonia"
label values ancestry ancestry

recode ancestry (1/2=0) (3=1), gen(ancestry2)

recode ancestry (3=1) (2=0.5) (1=0), gen(ancestry_cont)
label variable ancestry_cont "Ancestry"
label define ancestry_cont 1 "Two parents born in Catalonia" 0 "None of the parents born in Catalonia"
label values ancestry_cont ancestry_cont

**Age
rename EDAT age
label var age "Age (years)"

**Gender
recode SEXE (1=0 "men") (2=1 "women"), gen(female)
label var female "Female"

**Education 
recode ESTUDIS (1/4=1 "Primary") (5/7=2 "Secondary") (8/11=3 "University") (else=.), gen(education)
label variable education "Education"


**Ideology
recode IDEOL_0_10 (98/99=.), gen(ideol)
replace ideol= ideol/10
label var ideol "Ideology"

**Suppot secession 
recode ACTITUD_INDEPEND__NCIA (1=1 "a favor") (2=0 "en contra") (else=.), gen(secession)
label variable secession "Secession"

drop if BOP_NUM < 34

***********************************************

***Context variable
preserve
import excel "catalan_county.xlsx", first clear
sort COMARCA
save "catalan_county", replace

import excel "catalan_municipality.xlsx", first clear
rename MUN MUNICIPI
sort MUNICIPI
save "catalan_municipality", replace
restore

*merge municipalities
merge m:1 MUNICIPI using "catalan_municipality", keep(master match)
rename _merge merge

*merge counties
merge m:1 COMARCA using "catalan_county", keep(master match) 
drop _merge

*drop values from counties from which we have data at the municipality level
foreach var of varlist com_* {
replace `var'=. if merge==3
}

egen understand_cat= rowmean(com_understand mun_understand) 
egen speak_cat =rowmean(com_speak mun_speak)
egen read_cat =rowmean(com_read mun_read)
egen write_cat =rowmean(com_write mun_write)
egen notund_cat=rowmean(com_notund mun_notund)

foreach var of varlist understand_cat speak_cat read_cat write_cat notund_cat {
replace `var'  = `var'/100
}
label var speak_cat "% speaking Catalan county"
